//
//  DTDeviceAlert.m
//  PersonalManager
//
//  Created by user on 16/8/1.
//  Copyright © 2016年 Haiyun.Qian. All rights reserved.
//

#import "DTDeviceAlert.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface DTDeviceAlert()<NSObject,UIAlertViewDelegate,UITextFieldDelegate,UIActionSheetDelegate>
@property (nonatomic, strong) NSString * cancelButtonTittle;
@property (nonatomic, strong) NSArray *  otherButtonTittle;

@end
@implementation DTDeviceAlert

-(instancetype)init: (DTJavascriptCommand *)commond {
    self = [super init];
    self.command = commond;
    
    NSString *handleName = commond.handleName;
    _textFieldInput.delegate = self;
    _preLoadView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    if ([handleName  isEqualToString:@"device.notification.confirm"]) {
        self.tittle  = [commond getParamValue:@"title"];
        self.message = [commond getParamValue:@"message"];
        self.cancleTitle = [commond getParamValue:@"buttonName"];
        
        [self cancelOtherButtontittle:@"buttonLabels"];

    }else if ([handleName  isEqualToString:@"device.notification.actionSheet"]){
        self.cancelButtonTittle = [self.command getParamValue:@"cancelButton"];
        self.otherButtonTittle  = [self.command getParamValue:@"otherButtons"];

    }else if ([handleName  isEqualToString:@"device.notification.prompt"]){
        self.message = [commond getParamValue:@"title"];
        self.tittle = [commond getParamValue:@"message"];
        self.cancelButtonTittle = [self.command getParamValue:@"cancelButton"];
        self.confiremTittle  = [self.command getParamValue:@"otherButtons"];
    }
        
    
    return self;
}
#pragma tips 

-(void) cancelOtherButtontittle: (NSString *) arrayName {
    NSArray * array = [_command getParamValue:arrayName];
    if (array) {
        self.buttonLabels = array;
        if (self.buttonLabels.count >= 1) {
            self.cancleTitle = _buttonLabels[0];
        }
        if (self.buttonLabels.count >= 2) {
            self.confiremTittle = _buttonLabels[1];
        }
    }
}

#pragma mark - showAction
-(void)confirm {

    self.alertView = [[UIAlertView alloc]initWithTitle:_tittle message:_message delegate:self cancelButtonTitle:_cancleTitle otherButtonTitles:_confiremTittle, nil];
    [self.alertView show];
}


-(void) show {
//    NSString *tittle = [self.command getParamValue:@"title"];
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:_tittle delegate:self cancelButtonTitle:_cancelButtonTittle destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    
    for (int i = 0; i< _otherButtonTittle.count; i ++) {
        NSString *buttonTittle = _otherButtonTittle[i];
        
        [actionSheet addButtonWithTitle:buttonTittle];
    }
    [actionSheet showInView:_command.controller.view];
}

-(void) vibrate {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

-(void) prompt {

    self.alertView = [[UIAlertView alloc]initWithTitle:_tittle message:_message delegate:self cancelButtonTitle:_cancelButtonTittle otherButtonTitles:_confiremTittle, nil];
    
    [_alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *textFiled = [_alertView textFieldAtIndex:0];
    textFiled.placeholder = @"请输入一个名称";

    [_alertView show];
    
}

-(void) showPreloader {
    
    _preLoadView.center  = CGPointMake(_command.controller.view.center.x, _command.controller.view.center.y);
    _preLoadView.backgroundColor = [UIColor blueColor];
    [_preLoadView startAnimating];
    [_command.controller.view addSubview: _preLoadView];
    
}
-(void) hidePreloader {
    [_preLoadView stopAnimating];
    [_preLoadView hidesWhenStopped];
    [_preLoadView removeFromSuperview];
    
}
#pragma mark - UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    NSString *input = [_alertView textFieldAtIndex:0].text;

    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",(long)buttonIndex],@"buttonIndex",input,@"输入的名称是" ,nil];
    
    [self.command successRessponse:dic];

}
- (void)alertView:(UIAlertView *)alertView andDidDismissWithButtonIndex:(int)buttonIndex {
    [self.command cancelResponse:@""];

}
-(void)alertViewCancel:(UIAlertView *)alertView {
    [self.command cancelResponse:@""];
}

#pragma mark - actionSheetDelegate


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"-1",@"buttonIndex",_cancelButtonTittle,@"buttonTitle", nil];
        [_command successRessponse:dic];
    }else {
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",buttonIndex - 1],@"buttonIndex",_otherButtonTittle[buttonIndex - 1],@"buttonTitle", nil];
        [_command successRessponse:dic];
    }
}
-(void) actionSheetCancel: (UIActionSheet *)actionSheet {
    [_command cancelResponse:@""];
}

-(void) actionSheet: (UIActionSheet *)actionSheet andDidDismissWithButtonIndex: (int)buttonIndex {

    [self actionSheet:actionSheet clickedButtonAtIndex:buttonIndex];
}



@end
